package com.chen.scaffolding.nacosconfig.controller;

import com.chen.scaffolding.nacosconfig.entity.Authority;
import com.chen.scaffolding.nacosconfig.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试nacos-config
 * @author :master
 */
@RestController
@RefreshScope
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    private AuthorityService authorityService;

    @Value("${useLocalCache}")
    private String useLocalCache;

    /**
     * http://localhost:8080/config/get
     */
    @RequestMapping("/get")
    public String get() {
        return useLocalCache;
    }

    @RequestMapping("addAuthority")
    public String addAuthority (@RequestBody Authority authority) {
        authorityService.addAuthority(authority);
        return "权限设置成功！";
    }


    @RequestMapping("upateAuthority")
    public String upateAuthority (@RequestBody Authority authority) {
        try {
            authorityService.updateAuthority(authority);
            return "权限修改成功！";
        }catch (Exception e) {
            return e.getMessage();
        }
    }
}
